﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.ServiceModel;
using AutoMapper;
using Core;
using IglooCoder.Commons.WcfNhibernate;
using Infrastucture;
using WcfService1.DTO;

namespace WcfService1
{
    // NOTE: You can use the "Rename" command on the "Refactor" Warehouse to change the class name "WarehouseServices" in code, svc and config file together.
    [ServiceBehavior(InstanceContextMode = InstanceContextMode.PerCall)]
    [NHibernateContext]
    public class WarehouseServices : IWarehouseServices
    {
        private readonly WarehouseRepository WarehouseRepository;
        private readonly InventoryWarehouseRepository _iwRepository;
        private readonly WcfSessionStorage _session;

        public WarehouseServices()
        {
            _session = new WcfSessionStorage();
            WarehouseRepository = new WarehouseRepository(_session);
            _iwRepository = new InventoryWarehouseRepository(_session);
        }

        #region IWarehouseServices Members

        public List<WarehouseDTO> GetList(int page, out long recordCount, string sortProperty = null,
                                          string sortType = "ASC")
        {
            var Warehouses = new List<Warehouse>();
            var Results = new List<WarehouseDTO>();

            if (string.IsNullOrEmpty(sortProperty))
            {
                Warehouses = WarehouseRepository.GetDataPaged(page, 30).ToList();
            }
            else
            {
                Warehouses = WarehouseRepository.GetDataPaged(page, 30, sortProperty, sortType).ToList();
            }

            recordCount = WarehouseRepository.RecordCount;

            Warehouses.ForEach(x =>
                                   {
                                       var WarehouseDto = new WarehouseDTO();
                                       Mapper.Map(x, WarehouseDto);
                                       //AutoMapper.Mapper.Map(x.ParentWarehouse, WarehouseDto.ParentWarehouse);
                                       Results.Add(WarehouseDto);
                                   }
                );

            return Results;
        }

        public bool Save(WarehouseDTO WarehouseDTO)
        {
            bool res = false;
            try
            {
                WarehouseRepository.ExecuteTransactionNative(()=>
                                                                 {
                                                                     Warehouse Warehouse = WarehouseRepository.GetDataOne(x => x.KodeWarehouse == WarehouseDTO.KodeWarehouse);
                                                                     var user = new User();
                                                                     if (Warehouse == null)
                                                                     {
                                                                         Warehouse = new Warehouse();
                                                                         WarehouseDTO.created_date = DateTime.Now;
                                                                     }
                                                                     else
                                                                     {
                                                                         WarehouseDTO.modified_date = DateTime.Now;
                                                                     }
                                                                     Mapper.Map(WarehouseDTO, Warehouse);
                                                                     WarehouseRepository.SaveOrUpdate(Warehouse);
                                                                     res = true;         
                                                                 });
            }
            catch (Exception exception)
            {
                res = false;
            }

            return res;
        }


        public bool Delete(WarehouseDTO WarehouseDto)
        {
            bool res;
            try
            {
                var Warehouse = new Warehouse();
                Mapper.Map(WarehouseDto, Warehouse);
                WarehouseRepository.Delete(Warehouse);
                res = true;
            }
            catch (Exception)
            {
                res = false;
            }

            return res;
        }


        public List<WarehouseDTO> GetListByCriteria(Dictionary<String, Object> criteria, string expression, int page,
                                                    out long recordCount)
        {
            var Warehouses = new List<Warehouse>();
            var Results = new List<WarehouseDTO>();
            Warehouses = WarehouseRepository.GetDataPaged(criteria, expression, page, 30).ToList();
            recordCount = WarehouseRepository.RecordCount;
            Warehouses.ForEach(x =>
                                   {
                                       var WarehouseDto = new WarehouseDTO();
                                       Mapper.Map(x, WarehouseDto);
                                       Results.Add(WarehouseDto);
                                   }
                );

            return Results;
        }


        public List<WarehouseDTO> GetAllWarehouse()
        {
            IList<Warehouse> Warehouses = WarehouseRepository.FetchAll();
            IEnumerable<WarehouseDTO> Results = from w in Warehouses
                                                select
                                                    new WarehouseDTO
                                                        {
                                                            KodeWarehouse = w.KodeWarehouse,
                                                            WarehouseName = w.WarehouseName,
                                                            EmailFolder =  w.EmailFolder
                                                        };

            return Results.ToList();
        }


        public WarehouseDTO GetWarehouse(string kodeWarehouse)
        {
            Warehouse Warehouse = WarehouseRepository.GetDataOne(x => x.KodeWarehouse == kodeWarehouse);
            var WarehouseDto = new WarehouseDTO();
            Mapper.Map(Warehouse, WarehouseDto);
            return WarehouseDto;
        }

        public List<WarehouseDTO> GetWarehouseByInventory(InventoryDTO inventory)
        {
            if (string.IsNullOrEmpty(inventory.SerialNumber)) return new List<WarehouseDTO>();
            IList<InventoryWarehouse> Stoks = _iwRepository.GetDataList(x => x.SerialNumber == inventory.SerialNumber);
            IEnumerable<WarehouseDTO> Results = from w in Stoks
                                                select new WarehouseDTO {KodeWarehouse = w.KodeWarehouse};
            return Results.ToList();
        }

        #endregion
    }
}